package com.wangbl.infomanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wangbl.infomanager.utils.Logger;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class PushMsgListActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	private static final String TAG = "PushMsgListActivity";
	private ListView listview;
	private String url = "http://irmsma.ruc.edu.cn/dis.php";
	private List<String> keys;
	private SharedPreferences sp;
	private Map<String,?> map;
	private SharedPreferences sp_push_msg_state;
	private static final int REQ_CODE = 20;
	public static final int SUCCESS = 30;
	private BaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_push_msg);
		super.onCreate(savedInstanceState);
		title_user.setVisibility(View.GONE);
		title_text.setText("推送列表");
		sp = getSharedPreferences("push_info", MODE_PRIVATE);
		sp_push_msg_state = getSharedPreferences("push_msg_state", Context.MODE_PRIVATE);
		map = sp.getAll();
		Logger.i(TAG, "map.size:"+map.size());
		listview = (ListView) findViewById(R.id.list_push_msg);
		keys = new ArrayList<String>(map.keySet());
		listview.setOnItemClickListener(this);
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return keys.size();
		}

		@Override
		public Object getItem(int position) {
			return keys.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			Holder holder;
			String push_id;
			if (convertView != null) {
				view = convertView;
				holder = (Holder) view.getTag();
			} else {
				view = View.inflate(PushMsgListActivity.this,
						R.layout.item_listview_push_msg, null);
				holder = new Holder();
				holder.tv_content = (TextView) view.findViewById(R.id.tv_push_msg);
				holder.iv_news = (ImageView) view.findViewById(R.id.iv_push_msg_news);
				view.setTag(holder);
			}
			push_id = keys.get(position);
			String str = sp.getString(push_id,"");
			if(!TextUtils.isEmpty(str)){
				String[] strs = str.split("##");
				if(sp_push_msg_state.getBoolean(push_id, true)){
					holder.iv_news.setVisibility(View.INVISIBLE);
				} else {
					holder.iv_news.setVisibility(View.VISIBLE);
				}
				holder.tv_content.setText(strs[1]);
			}
			return view;
		}

	}

	private class Holder {
		public TextView tv_content;
		public ImageView iv_news;
	}

	@Override
	protected void interrupt() {

	}

	@Override
	public void onClick(View v) {
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this, InfoDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("push_id", keys.get(position));
		intent.putExtras(bundle);
		startActivityForResult(intent, REQ_CODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		adapter.notifyDataSetChanged();
		if(requestCode == REQ_CODE && resultCode == SUCCESS){
			
		}
	}

}
